/**
 * @param {number} numCourses
 * @param {number[][]} prerequisites
 * @return {boolean}
 */
function canFinish(numCourses, prerequisites) {
    const g = new Array(numCourses).fill(null).map(() => new Set())
    const indegrees = new Array(numCourses).fill(0)

    for (let i = 0; i < prerequisites.length; i++) {
        const [w, v] = prerequisites[i]
        g[v].add(w)
        indegrees[w]++
    }

    const queue = []
    let index = 0
    for (let i = 0; i < numCourses; i++) {
        if (indegrees[i] === 0) queue.push(i)
    }

    while (queue.length > 0) {
        const v = queue.shift()
        index++
        for (let w of g[v]) {
            indegrees[w]--
            if (indegrees[w] === 0) queue.push(w)
        }
    }

    if (index !== numCourses) {
        return false
    } else {
        return true
    }
}